//
//  BLSubscribeViewController.m
//  百思不得姐
//
//  Created by hbl on 16/3/19.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLSubscribeViewController.h"
#import "MJExtension.h"
#import "BLNetwork.h"
#import "BLSubsribe.h"
#import "BLSubsribeTableViewCell.h"
#import "MJRefresh.h"
#import "SVProgressHUD.h"
@interface BLSubscribeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *subscribeTableView;
@property (nonatomic, strong)NSArray *subscribeArray;
@end

@implementation BLSubscribeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubsribleTableView];
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [self getData];
    self.view.backgroundColor = BLBg;
}

- (void)getData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"tag_recommend";
    params[@"action"] = @"sub";
    params[@"c"] = @"topic";
    
    [BLNetwork GET:@"http://api.budejie.com/api/api_open.php" parameter:params success:^(id result) {
        NSArray *tempArray = result;
        self.subscribeArray = [BLSubsribe mj_objectArrayWithKeyValuesArray:tempArray];
        [self.subscribeTableView reloadData];
        [self.subscribeTableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"加载失败！"];
    }];
}

- (void)setupSubsribleTableView
{
    self.subscribeTableView.contentInset = UIEdgeInsetsMake(5, 0, 5, 0);
    self.subscribeTableView.rowHeight = 70;
    self.subscribeTableView.backgroundColor = [UIColor clearColor];
    [self.subscribeTableView registerNib:[UINib nibWithNibName:@"BLSubsribeTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"subscribe"];
    self.subscribeTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
}

- (void)refresh
{
    [self getData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.subscribeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BLSubsribe *subsribe = [[BLSubsribe alloc]init];
    subsribe = self.subscribeArray[indexPath.row];
    BLSubsribeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subscribe"];
    cell.subscribe = subsribe;
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
